package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : MessageTypeFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 14:59
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.DingDanMessageAdapter;
import com.chanjiedata.changbs.adapter.JiangLiMessageAdapter;
import com.chanjiedata.changbs.adapter.SystemMessageAdapter;
import com.chanjiedata.changbs.adapter.TiXianMessageAdapter;
import com.chanjiedata.changbs.base.BaseFragment;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.WebApis;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.MemberOuterClass;
import com.chanjiedata.changbs.web.ui.WebViewActivity;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by AndroidIntexh1 on 2018/6/4.
 */

public class MessageTypeFragment extends BaseFragment {
    @BindView(R.id.fragment_recycleView)
    RecyclerView mRecycleView;
    Unbinder unbinder;

    private int page = 1; // 当前页数
    private int pagesize = 10; // 当前页数
    private int position;  // 当前viewPage的位置 0:提现消息 1:奖励消息 2:担保消息 3:系统消息

    private List<MemberOuterClass.Pd_cash_list.Pd_cash_list_info> tiXianMessageBeans = new ArrayList<>(); // 提现消息
    private TiXianMessageAdapter txAdapter; // 提现消息适配器


    private List<MemberOuterClass.System_log.System_log_list> jiangLiMessageBeans = new ArrayList<>(); // 奖励消息
    private JiangLiMessageAdapter jlAdapter; // 奖励消息适配器

    private List<MemberOuterClass.Order_list.List_info> dingDanMessageBeans = new ArrayList<>(); // 担保消息
    private DingDanMessageAdapter ddAdapter; // 订单消息适配器

    private List<MemberOuterClass.System_log.System_log_list> systemMessageBeans = new ArrayList<>(); // 系统消息
    private SystemMessageAdapter stAdapter; // 系统消息适配器

    public static MessageTypeFragment NewInstance(int instanceType) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        MessageTypeFragment messageTypeFragment = new MessageTypeFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        mRecycleView.setBackgroundColor(Color.parseColor("#F2F3F4"));
        initRecycle();
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        if (position == 0) {
            NetworkManager.INSTANCE.post(Apis.pd_cash_list_log, page, new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) throws InvalidProtocolBufferException {

                    List<MemberOuterClass.Pd_cash_list.Pd_cash_list_info> lists = MemberOuterClass.Pd_cash_list.parseFrom(response).getPdCashListInfoList();
                    txAdapter.addData(lists);
                    int size = lists.size();
                    if (size < 10) {
                        txAdapter.loadMoreEnd(true);
                    } else {
                        txAdapter.loadMoreComplete();
                    }
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
//                    showToast(errorMessage);
                }
            });

        } else if (position == 1) {
            NetworkManager.INSTANCE.post(Apis.reward_log, page, new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) throws InvalidProtocolBufferException {
                    List<MemberOuterClass.System_log.System_log_list> datas = MemberOuterClass.System_log.parseFrom(response).getSystemLogListList();
                    jlAdapter.addData(datas);
                    int size = datas.size();
                    if (size < 10) {
                        jlAdapter.loadMoreEnd(true);
                    } else {
                        jlAdapter.loadMoreComplete();
                    }
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
//                    showToast(errorMessage);
                }
            });

        } else if (position == 2) {
            NetworkManager.INSTANCE.post(Apis.order_list, page, new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) throws InvalidProtocolBufferException {
                    List<MemberOuterClass.Order_list.List_info> datas = MemberOuterClass.Order_list.parseFrom(response).getListInfoList();
                    ddAdapter.addData(datas);
                    int size = datas.size();
                    if (size < 10) {
                        ddAdapter.loadMoreEnd(true);
                    } else {
                        ddAdapter.loadMoreComplete();
                    }
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
//                    showToast(errorMessage);
                }
            });
        } else if (position == 3) {
            NetworkManager.INSTANCE.post(Apis.system_log, page, new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) throws InvalidProtocolBufferException {
                    List<MemberOuterClass.System_log.System_log_list> datas = MemberOuterClass.System_log.parseFrom(response).getSystemLogListList();
                    stAdapter.addData(datas);
                    int size = datas.size();
                    if (size < 10) {
                        stAdapter.loadMoreEnd(true);
                    } else {
                        stAdapter.loadMoreComplete();
                    }
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
//                    showToast(errorMessage);
                }
            });

        }


    }


    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        if (position == 0) {
            mRecycleView.setAdapter(txAdapter = new TiXianMessageAdapter(mContext, tiXianMessageBeans));
            txAdapter.bindToRecyclerView(mRecycleView);
            txAdapter.setEmptyView(R.layout.recycle_empty);
            txAdapter.setOnItemClickListener((adapter, view, position) -> {
                MemberOuterClass.Pd_cash_list.Pd_cash_list_info dataBean = tiXianMessageBeans.get(position);
                Intent intent = new Intent(mContext, TiXianMessageDetailActivity.class);
                intent.putExtra("pdc_id", dataBean.getPdcId() + "");
                startActivity(intent);
            });
            txAdapter.setOnLoadMoreListener(() -> {
                page = txAdapter.getData().size() / pagesize + 1;
                loadData();
            }, mRecycleView);

        } else if (position == 1) {
            mRecycleView.setAdapter(jlAdapter = new JiangLiMessageAdapter(mContext, jiangLiMessageBeans));
            jlAdapter.bindToRecyclerView(mRecycleView);
            jlAdapter.setEmptyView(R.layout.recycle_empty);
            jlAdapter.setOnLoadMoreListener(() -> {
                page = jlAdapter.getData().size() / pagesize + 1;
                loadData();
            }, mRecycleView);

        } else if (position == 2) {
            mRecycleView.setAdapter(ddAdapter = new DingDanMessageAdapter(mContext, dingDanMessageBeans));
            ddAdapter.bindToRecyclerView(mRecycleView);
            ddAdapter.setEmptyView(R.layout.recycle_empty);
            ddAdapter.setOnItemClickListener((adapter, view, position) -> {
                MemberOuterClass.Order_list.List_info dataBean = dingDanMessageBeans.get(position);
                WebViewActivity.startActivity(mContext, WebApis.orderDetail + "?id=" + dataBean.getOrderId());
            });

            ddAdapter.setOnLoadMoreListener(() -> {
                page = ddAdapter.getData().size() / pagesize + 1;
                loadData();
            }, mRecycleView);
        } else if (position == 3) {
            mRecycleView.setAdapter(stAdapter = new SystemMessageAdapter(mContext, systemMessageBeans));
            stAdapter.bindToRecyclerView(mRecycleView);
            stAdapter.setEmptyView(R.layout.recycle_empty);
            stAdapter.setOnItemClickListener((adapter, view, position) -> {
                MemberOuterClass.System_log.System_log_list dataBean = systemMessageBeans.get(position);
                Intent intent = new Intent(mContext, SystemMessageDetailActivity.class);
                intent.putExtra("time", dataBean.getPushTime());
                intent.putExtra("content", dataBean.getPushMessage());
                intent.putExtra("title", dataBean.getPushTitle());
                startActivity(intent);
            });
            stAdapter.setOnLoadMoreListener(() -> {
                page = stAdapter.getData().size() / pagesize + 1;
                loadData();
            }, mRecycleView);
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }
}
